package client.comm.baoding.ui

import android.content.ClipData
import android.content.ClipboardManager
import android.view.LayoutInflater
import android.view.View
import androidx.databinding.DataBindingUtil
import client.comm.commlib.dialog.CameraOrSelectDialog
import client.comm.commlib.toast.toast
import client.comm.commlib.utils.StoreUtil
import client.comm.baoding.R
import client.comm.baoding.api.bean.FzInter
import client.comm.baoding.api.bean.GoodsOrder
import client.comm.baoding.databinding.ActivityCurgoodsorderBinding
import client.comm.baoding.databinding.LayoutSktyEeBinding
import client.comm.baoding.dialog.MsgTitleDialog
import client.comm.baoding.ui.vm.CurGoodsOrderViewModel
import client.comm.baoding.api.bean.UserInfo
import client.comm.baoding.databinding.LayoutGoodswrapitemBinding
import client.comm.baoding.BaseMiddleActivity
class CurGoodsOrderActivity : BaseMiddleActivity<CurGoodsOrderViewModel, ActivityCurgoodsorderBinding>() {
    override fun getLayout(): Int {
        return R.layout.activity_curgoodsorder
    }

    override fun initViewModel(): CurGoodsOrderViewModel = createViewModel()

    override fun initView() {
        binding.event = this
        binding.vm = viewModel
        val bean = intent.getParcelableExtra<GoodsOrder.Order>("bean")
        binding.bean = bean

        bean?.let {
            mImgLoader.load(it.order_img).into(binding.img)

            binding.itemWrap.removeAllViews()
            for (i in it.goods_list) {
                DataBindingUtil.inflate<LayoutGoodswrapitemBinding>(
                    LayoutInflater.from(this),
                    R.layout.layout_goodswrapitem,
                    binding.itemWrap,
                    false
                ).apply {

                    this.bean = i
                    mImgLoader.load(i.goods_img).into(img)
                    binding.itemWrap.addView(root)
                }
            }

            binding.emptyLayout.visibility = View.GONE
        }

        StoreUtil.getBean<UserInfo>(this)?.let {
            binding.skWrap.removeAllViews()
            for (i in it.pay_list) {
                val bindingType = DataBindingUtil.inflate<LayoutSktyEeBinding>(
                    LayoutInflater.from(this),
                    R.layout.layout_skty_ee,
                    binding.skWrap,
                    false
                ).apply {
                    i.pay_img?.let {
                        mImgLoader.load(i.pay_img).into(img)
                        img.visibility = View.VISIBLE
                    }

                    i.pay_name?.let {
                        name = i.pay_name
                        oneWrap.visibility = View.VISIBLE

                    }

                    i.pay_card?.let {
                        card = i.pay_card
                        twoWrap.visibility = View.VISIBLE

                    }

                    i.real_name?.let {
                        realName = i.real_name
                        threeWrap.visibility = View.VISIBLE
                    }

                    event = FzInter {
                        val cmb = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
                        cmb.setPrimaryClip(ClipData.newPlainText(null, it))
                        toast("已复制")
                    }
                }
                binding.skWrap.addView(bindingType.root)
            }
            binding.emptyLayout.visibility = View.GONE

        }

        viewModel.cancleCall.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    finish()
                    toast("已取消")
                }
            }
        })

    }


    override fun observerUI() {
        viewModel.okCall.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    toast("上传成功")
                    finish()
                } else {
                    toast(it.message)
                }
            }
        })
    }

    fun imgSelect() {
        CameraOrSelectDialog(this).apply {
            suc = {
                viewModel.imgOne.value = it
                mImgLoader.load(it).into(this@CurGoodsOrderActivity.binding.img)
            }
        }.show()
    }

    fun ok() {
        if (viewModel.imgOne.value == null || binding.bean == null) {
            toast("所有不得为空")
            return
        }
        viewModel.ok(binding.bean as GoodsOrder.Order, this@CurGoodsOrderActivity)
    }

    fun cancle() {
        MsgTitleDialog(this).apply {
            okTxt = "确定"
            cancleTxt = "不"
            title = "确定取消订单？"
            ok = {
                viewModel.cancle("${this@CurGoodsOrderActivity.binding.bean!!.order_id}")
            }
        }.show()
    }
}